var mysql = require('mysql');
var express = require('express');
var app = express();
var connection = mysql.createConnection({
  host: '127.0.0.1',
  port: '3306',
  user: 'root',
  password: 'admin',
  database: 'db_message'
})
connection.connect();
var getSql_cet4 = 'SELECT * FROM cet where level = 4'
var getSql_cet6 = 'SELECT * FROM cet where level = 6'
var cet4_str = '';
var cet6_str = '';

var getCet4 = function() {
  str = connection.query(getSql_cet4, function (err, result) {
    if (err)
      console.log('SELECT ERROR:' + err);
      cet4_str = JSON.stringify(result);
  });
}
var getCet6 = function() {
  str = connection.query(getSql_cet6, function (err, result) {
    if (err)
      console.log('SELECT ERROR:' + err);
      cet6_str = JSON.stringify(result);
  });
}
getCet4();
getCet6();
app.get('/cet4', function (req, res) {
  res.setHeader("Access-Control-Allow-Origin", "*");
  res.send(cet4_str);
});
app.get('/cet6', function (req, res) {
  res.setHeader("Access-Control-Allow-Origin", "*");
  res.send(cet6_str);
});

app.listen(3001, function () {
  console.log("Server running at 3000 port");
})